function similarity_eigs(file_name, num_movies, num_features)
% Run Principal Components Analysis on similarity matrix and get the first
% num_features principal components (the eigenvectors of the covariance matrix).
% The similarity matrix in file_name must be num_movies x num_movies, stored
% in ASCII format.
%
% Intended to be called from the command line. Example:
%   matlab -nojvm -nodisplay -r "similarity_eigs('../test',4,2);exit"
%
% For the 17770 by 17770 matrix, this takes about 3GB of RAM.
%

S = dlmread(file_name, '');
% HACK: cope with bug in TF/DF similarity code (extra column).
if size(S) == [num_movies num_movies+1]
  S = S(:,1:num_movies);
end
C = cov(reshape(S, num_movies, num_movies));
[V,D] = eigs(C, num_features);

dlmwrite([file_name '.eigvals'], diag(D), ' ')
dlmwrite([file_name '.eigvecs'], V, ' ')
